package cn.yunyichina.provider.clinic.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import cn.yunyichina.provider.clinic.constant.BizConstant;
import cn.yunyichina.provider.clinic.hisiface.vo.ClinicVo;
import cn.yunyichina.provider.clinic.hisiface.vo.ResponseGetMzfeelistVo;
import cn.yunyichina.provider.clinic.others.hisiface.MZFeeDetail;
import cn.yunyichina.provider.clinic.vo.ListMzfeeVoResponse;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.iface.entity.cache.SessionInfo;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.hisiface.clinic.FeeDetailVo;
import cn.yunyichina.provider.iface.entity.hisiface.clinic.FeeDetailVoResponse;
import cn.yunyichina.provider.iface.entity.hisiface.clinic.FeeVo;
import cn.yunyichina.provider.iface.entity.hisiface.clinic.FeeVoResponse;
import cn.yunyichina.utils.convert.JsonUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


public class ClinicBizManager {
    private Logger logger = LoggerFactory.getLogger(ClinicBizManager.class);

    public List<FeeVoResponse> getMZFeeListEx(ClinicVo clinicvo, String sessionId) {
        List<FeeVoResponse> respList = new ArrayList<FeeVoResponse>();
        try {
            FeeVo feevo = new FeeVo();
            feevo.setHospitalCode(clinicvo.getHospitalCode());
            feevo.setBranchCode(clinicvo.getBranchCode());
            feevo.setPatCardNo(clinicvo.getPatCardNo());
            feevo.setPatCardType("1");
            feevo.setSessionId(sessionId);
            logger.info("getMZFeeListEx, listClinicFee request=" + JSON.toJSONString(feevo));
            ResCommon<List<FeeVoResponse>> feeResponse = HisifaceService.getHisifaceClient().listClinicFee(feevo);
            logger.info("getMZFeeListEx, listClinicFee feeResponse=" + JSONObject.toJSONString(feeResponse));
            if (!ResCommon.SUCCESS_CODE.equals(feeResponse.getResultCode())) {
                throw new RuntimeException(
                        "查询就诊卡号" + clinicvo.getPatCardNo() + "对应的待缴费列表失败：" + feeResponse.getResultMessage() + "！");
            }
            List<FeeVoResponse> feeVoResponses = JsonUtils.parseObject(feeResponse.getResult(), List.class);
            logger.info("listMzfeeOut->获取待缴费列表:" + JSON.toJSONString(feeVoResponses));

            for (int i = 0; i < feeVoResponses.size(); i++) {
                FeeVoResponse fee = new FeeVoResponse();
                FeeVoResponse feeTmp = JsonUtils.parseObject(
                        JSON.toJSONString(feeVoResponses.get(i)), FeeVoResponse.class);
                BeanUtils.copyProperties(feeTmp, fee);
                respList.add(fee);
            }
        } catch (Exception ex) {
            logger.error("getMZFeeListEx error", ex);
        }
        return respList;
    }

    /**
     * 查询待缴费明细
     * @param userInfo 
     * 
     * @param params
     * @return
     */
    public List<FeeDetailVoResponse> getMZFeeDetail(ListMzfeeVoResponse mzFee, SessionInfo userInfo, String sessionId) {
        FeeDetailVo feeDetailVo = new FeeDetailVo();
        feeDetailVo.setBranchCode(mzFee.getBranchCode());
        feeDetailVo.setHospitalCode(mzFee.getHospitalCode());
        feeDetailVo.setDeptCode(mzFee.getDeptCode());
        feeDetailVo.setDoctorCode(mzFee.getDoctorCode());
        feeDetailVo.setMzFeeId(mzFee.getMzFeeId());
        Map<String, Object> extMap = new HashMap<String, Object>();
        extMap.put("time", mzFee.getTime());
        extMap.put("pat_card_no", mzFee.getPatCardNo());
        feeDetailVo.setExtendParams(JsonUtils.toJsonString(extMap));
        logger.info("查询待缴费明细, 就诊卡号" + mzFee.getPatCardNo() + ", 门诊流水号" + mzFee.getMzFeeId()
                + ", 调用hisiface模块, 门诊待缴费记录明细查询接口入参request=" + JSON.toJSONString(feeDetailVo));
        ResCommon<List<FeeDetailVoResponse>> response = HisifaceService.getHisifaceClient()
                .getClinicFeeDetail(feeDetailVo);
        logger.info("查询待缴费明细, 就诊卡号" + mzFee.getPatCardNo() + ", 门诊流水号" + mzFee.getMzFeeId()
                + ", 调用hisiface模块, 门诊待缴费记录明细查询接口返回response=" + JSONObject.toJSONString(response));
        if (!ResCommon.SUCCESS_CODE.equals(response.getResultCode())) {
            logger.info("查询待缴费明细, 就诊卡号" + mzFee.getPatCardNo() + ", 门诊流水号" + mzFee.getMzFeeId() + ", 查询待缴费明细失败");
//            throw new RuntimeException(
//                    "查询就诊卡号" + mzFee.getPatCardNo() + "待缴费明细失败：" + response.getResultMessage() + "！");
        }
        return response.getResult();
    }

}
